document.write("<script language='javascript' src='/resources/js/views/defineTable.js'></script>");

var host_path_action_report_analysis_indicatorType= "/report/analysis/indicatorType.do";
var host_path_action_report_analysis_indicator = "/report/analysis/indicator.do";

var page = 1;
var pageSize = 30;

function queryByFormsInameAreaIndicatorType(curPage, pageSize, fromPage, ttIndicatorSelectCache){

    if(pageSize==null){
        pageSize=30;
    }
    if(curPage==null){
        curPage=page;
    }

    var indicatorType = $('#indicatorTypeSelect').combobox('getValues')+"";
    var level = getLevelByPageFrom(fromPage);
    var areaid = getAreaByPageFrom(fromPage);
    var formsid=$('#formid').combobox('getValue');
    var iname = $('#iname').textbox('getValue');
    var years=$('#comboboxYear').combobox('getText');

    console.info(indicatorType);
    console.info(level);
    console.info(areaid);
    console.info(formsid);
    console.info(iname);
    console.info(years);

    $.get(host_path_action_report_analysis_indicatorType, {"iname":iname, "indicatorType":indicatorType, "level":level, "years":years, "areaid":areaid, "formsid":formsid, "page":curPage, "pageSize":pageSize}, function (json) {

        if(json.code!=200){
            $('#ttIndicator').datagrid( {data:[]});
            return;
        }
        isTrue = true;
        var data=json.data;

        $('#ttIndicator').datagrid( {
            data:data.results,
            fitColumns:true,
            columns: [
                defineTableIndicatorNamesModel
            ],
            pagination:true,
            onLoadSuccess: function () {
                loadIndicatorReportInfo();
            },
            onSelect: function(index, row){
                loadIndicatorReportInfo();
            },
            onSelectAll: function(index, row){
                loadIndicatorReportInfo();
            },
            onUnselect: function (rowIndex, rowData) {
                moveCache(rowData.id);
                loadIndicatorReportInfo();
            },
            onUnselectAll: function(index, row){
                ttIndicatorSelectCache=[];
                loadIndicatorReportInfo();
            }
        });

        var total=data.size;
        var dataLength=data.results.length;
        var totalPage = Math.ceil(total / pageSize);
        var start = (curPage-1) * pageSize;
        var pg = $("#ttIndicator").datagrid("getPager");
        $(pg).pagination({
            total: total,
            pageList: [30, 500, 1000, 1500],
            pageSize: pageSize,
            pageNumber: curPage,
            beforePageText: '第',
            afterPageText: '页    共 ' + totalPage + ' 页',
            displayMsg: '当前显示 ' + (start + 1) + '-' + (start + dataLength) + ' 条记录 共 ' + total + ' 条记录',
            onSelectPage: function (curPage, pageSize) {
                queryByFormsInameAreaIndicatorType(curPage,pageSize,fromPage,ttIndicatorSelectCache)
            },
            onChangePageSize: function (ps) {
                pageSize = ps;
            }
        });
    },'json');
}


function queryByFormsInameAreaIndicators(curPage, pageSize, fromPage, ttIndicatorSelectCache) {

    curPage = parseInt(curPage);
    pageSize = parseInt(pageSize);
    console.info(fromPage);

    var yeares =$('#comboboxYear').combobox('getText');
    var yearList= [];
    if(yeares!=null ||yeares!='' || yeares!=undefined){
        yearList=yeares.split(',');
    }
    if(yearList.length>1){
        yearList.sort(funcSort);
    }
    var reportShowColumns = [];
    var ck = {
        title : '',
        field : 'ck',
        checkbox : true,
        width : 90
    };
    reportShowColumns.push(ck);
    reportShowColumns.push(defineTableReportAllColumns["iname"]);//指标名称
    reportShowColumns.push(defineTableReportAllColumns["code"]);//指标名称
    reportShowColumns.push(defineTableReportAllColumns["unit"]);//计量单位
    reportShowColumns.push(defineTableReportAllColumns["places"]);//小数位数

    for (var i = 0; i < yearList.length; i++) {
        var c = {title : yearList[i], field : yearList[i], width : 90,align:"center",
            formatter : function(value, row, index) {
                // 数据上报的类型_每年的目标值_数值_显示数据展示时效果的规则_每年的备注
                var tempValue;
                if(value!=null && value.indexOf('_')!=-1){
                    tempValue = value.split('_')[2];
                }
                if(tempValue===null||tempValue==""||tempValue=="null"||tempValue=="NULL"){
                    return "";
                }else{
                    return tempValue;
                }
            }
        };
        reportShowColumns.push(c);
    }
    reportShowColumns.push(defineTableReportAllColumns["sname"]);//资料来源

    var indicators=0;
    var row=ttIndicatorSelectCache;
    if(row!="" && row !='' && row.length>0){
        for (var i = 0; i < row.length; i ++){
            indicators+=row[i]['indicator']+",";
        }
        indicators=indicators.substring(0, indicators.length-1);
    }

    var level = getLevelByPageFrom(fromPage);
    var areaid = getAreaByPageFrom(fromPage);

    console.info(level);
    console.info(areaid);

    var formsid=$('#formid').combobox('getValue');

    $.get(host_path_action_report_analysis_indicator, {"years": yeares,"indicators":indicators, "areaid":areaid,"level":level,"page": curPage, "pageSize": pageSize,"formsid":formsid}, function (json) {
        if(json.code!=200) {
            clearData();
            return;
        }
        var data = json.data;
        var total= 0;
        if(data!=null){
            total = data.size;
        }
        var totalPage = Math.ceil(total / pageSize);
        var start = (curPage - 1) * pageSize;
        var dataLength = 0;
        data = data["results"];
        if (data != null) {
            dataLength = data.length;
        }
        $('#annualAnalysis_tt').datagrid( {
            fitColumns: true,
            frozen:true,
            columns:[
                reportShowColumns
            ],
            collapsible: true,
            singleSelect : false,// 是否单选
            checkOnSelect: true,//选中行后,checkbox选中
            selectOnCheck: true,//选中行后,checkbox选中
            loadMsg: '数据加载中,请稍后......',
//              删除之后，不显示总数据数的排序号，只显示本页数的排序号
            pageNumber: curPage,//显示数据列用
            pageSize: pageSize,
            pageList: [30, 500, 1000, 1500],
            // rownumbers: isShow,
//              删除之后，不显示总数据数的排序号，只显示本页数的排序号
            pagination: true,//打开翻页功能
            striped:true,//表格奇偶渐变色
            data: data,
            onClickRow:function(index, data) {
                //显示页面效果
                $("#annualAnalysis_containerzhuzi").height("360px");
                $("#annualAnalysis_containerzhuzi").width("98%");
                // show.tableshow();
                $("#annualAnalysis_chart").panel("expand",true);
                $("#annualAnalysis_colorSelect").css({display:"block"});
                $("#none").css({display:"block"});
                $("#tabb").css({marginTop:"0px"});

                showFieldAnalysis();
            }
        });

        var p = $('#annualAnalysis_tt').datagrid('getPager');
        $(p).pagination({
            total: total,
            pageList: [30, 500, 1000, 1500],
            pageSize: pageSize,
            pageNumber: curPage,
            beforePageText: '第',
            afterPageText: '页    共 ' + totalPage + ' 页',
            displayMsg: '当前显示 ' + (start + 1) + '-' + (start + dataLength) + ' 条记录 共 ' + total + ' 条记录',
            onSelectPage: function (curPage, pageSize) {
                queryByFormsInameAreaIndicators(curPage, pageSize, fromPage, ttIndicatorSelectCache);
            },
            onChangePageSize: function (ps) {
                pageSize = ps;
            }
        });
    }, 'json');
}

// 用于保存选中的数据（翻页）
var ttIndicatorSelectValue;
function loadIndicatorReportInfo(){
    var row=$('#ttIndicator').datagrid('getSelections');
    ttIndicatorSelectCache = putCache(row);
    console.info(ttIndicatorSelectCache);
    $('#ttIndicatorSelect').datagrid( {
        data:ttIndicatorSelectCache,
        fitColumns:true,
        columns: [
            defineTableIndicatorOnlyNamesModel
        ]
    });
}

function getLevelByPageFrom(fromPage){
    var level=null;
    if(fromPage==ENUM_AREA_PROVINCE){
        return ENUM_AREA_LEVEL_PROVINCE;
    }else if(fromPage==ENUM_AREA_CITY){
        return ENUM_AREA_LEVEL_CITY;
    }else if(fromPage==ENUM_AREA_COUNTY){
        return ENUM_AREA_LEVEL_COUNTY;
    }
    return level;
}

function getAreaByPageFrom(fromPage){
    var areaid=null;
    if(fromPage==ENUM_AREA_PROVINCE){
        return areaid;
    }else if(fromPage==ENUM_AREA_CITY){
        return $('#fieldAnalysis_citySelect').combobox('getValue');
    }else if(fromPage==ENUM_AREA_COUNTY){
        return $('#countySelect').combobox('getValue');
    }
    return areaid;
}
